% 仿真参数设置
Nt = 4;          % 发送天线数
Nr = 4;          % 接收天线数
SNR_dB = 0:2:20; % SNR范围
num_trials = 1000; % 每个SNR点的Monte Carlo试验次数

% 初始化BER存储
ber_zf = zeros(size(SNR_dB));
ber_mmse = zeros(size(SNR_dB));
ber_qr = zeros(size(SNR_dB));
ber_sorted_qr = zeros(size(SNR_dB));
ber_mmse_qr = zeros(size(SNR_dB));
ber_sphere = zeros(size(SNR_dB));
ber_vblast = zeros(size(SNR_dB));

% Monte Carlo仿真
for snr_idx = 1:length(SNR_dB)
    SNR = SNR_dB(snr_idx);
    errors_zf = 0;
    errors_mmse = 0;
    errors_qr = 0;
    errors_sorted_qr = 0;
    errors_mmse_qr = 0;
    errors_sphere = 0;
    errors_vblast = 0;
    total_bits = 0;
    
    for trial = 1:num_trials
        % 生成MIMO系统
        [y, H, x] = generate_mimo_system(Nt, Nr, SNR);
        
        % 各种检测器
        x_est_zf = zf_detector(y, H);
        x_est_mmse = mmse_detector(y, H, SNR);
        x_est_qr = qr_detector(y, H);
        x_est_sorted_qr = sorted_qr_detector(y, H);
        x_est_mmse_qr = mmse_qr_detector(y, H, SNR);
        x_est_sphere = sphere_detector(y, H, sqrt(10));  % 设置合适的搜索半径
        x_est_vblast = vblast_detector(y, H, SNR);
        
        % 统计错误
        errors_zf = errors_zf + sum(abs(sign(real(x)) - sign(real(x_est_zf))) + ...
                                  abs(sign(imag(x)) - sign(imag(x_est_zf))));
        errors_mmse = errors_mmse + sum(abs(sign(real(x)) - sign(real(x_est_mmse))) + ...
                                      abs(sign(imag(x)) - sign(imag(x_est_mmse))));
        errors_qr = errors_qr + sum(abs(sign(real(x)) - sign(real(x_est_qr))) + ...
                                   abs(sign(imag(x)) - sign(imag(x_est_qr))));
        errors_sorted_qr = errors_sorted_qr + sum(abs(sign(real(x)) - sign(real(x_est_sorted_qr))) + ...
                                                 abs(sign(imag(x)) - sign(imag(x_est_sorted_qr))));
        errors_mmse_qr = errors_mmse_qr + sum(abs(sign(real(x)) - sign(real(x_est_mmse_qr))) + ...
                                             abs(sign(imag(x)) - sign(imag(x_est_mmse_qr))));
        errors_sphere = errors_sphere + sum(abs(sign(real(x)) - sign(real(x_est_sphere))) + ...
                               abs(sign(imag(x)) - sign(imag(x_est_sphere))));
        errors_vblast = errors_vblast + sum(abs(sign(real(x)) - sign(real(x_est_vblast))) + ...
                                abs(sign(imag(x)) - sign(imag(x_est_vblast))));
        total_bits = total_bits + 4*Nt;
    end
    
    % 计算BER
    ber_zf(snr_idx) = errors_zf / total_bits;
    ber_mmse(snr_idx) = errors_mmse / total_bits;
    ber_qr(snr_idx) = errors_qr / total_bits;
    ber_sorted_qr(snr_idx) = errors_sorted_qr / total_bits;
    ber_mmse_qr(snr_idx) = errors_mmse_qr / total_bits;
    ber_sphere(snr_idx) = errors_sphere / total_bits;
    ber_vblast(snr_idx) = errors_vblast / total_bits;
end

% 绘制性能曲线
figure;
semilogy(SNR_dB, ber_zf, 'o-', 'LineWidth', 2);
hold on;
semilogy(SNR_dB, ber_mmse, 's-', 'LineWidth', 2);
semilogy(SNR_dB, ber_qr, 'd-', 'LineWidth', 2);
semilogy(SNR_dB, ber_sorted_qr, '^-', 'LineWidth', 2);
semilogy(SNR_dB, ber_mmse_qr, 'v-', 'LineWidth', 2);
semilogy(SNR_dB, ber_sphere, 'p-', 'LineWidth', 2);
semilogy(SNR_dB, ber_vblast, 'h-', 'LineWidth', 2);
grid on;
xlabel('SNR (dB)');
ylabel('BER');
legend('ZF', 'MMSE', 'QR', 'Sorted QR', 'MMSE-QR', 'Sphere', 'VBLAST');
title('MIMO Detection Performance Comparison'); 